<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible.sync="showDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="formDate" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formDate.name"
          style="width:80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formDate.code"
          style="width:80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formDate.manager"
          style="width:80%"
          placeholder="请选择"
          @focus="getEmployeeSimple"
        >
          <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formDate.introduce"
          style="width:80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
const defultform = {
  name: '',
  code: '',
  manager: '',
  introduce: ''
}
export default {
  name: 'AddDept',
  props: {
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rules, value, callback) => {
      // 1.找到所有的部门数据
      const { depts } = await getDepartments()
      // 2.找到爹的儿子 ，去判断名称是否重复
      let isRpepeat = false
      if (this.formData) {
        isRpepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.treeNode.pid).some(item => item.name === value)
      } else {
        isRpepeat = depts.filter((item) => item.pid === this.treeNode.id).some((item) => item.name === value)
      }
      isRpepeat ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
    }
    const checkCodeRepeat = async(rules, value, callback) => {
      // 1.找到所有的部门数据
      const { depts } = await getDepartments()
      // 2.找到爹的儿子 ，去判断名称是否重复
      let isRpepeat = false
      if (this.formData) {
        isRpepeat = depts.some(item => item.id !== this.formData.id && item.code === value && value)
      } else {
        isRpepeat = depts.some((item) => item.code === value && value)
      }

      isRpepeat ? callback(new Error(`组织架构中已经有部门使用${value}的部门了`)) : callback()
    }

    return {
      showDialog: false,
      formDate: {
        ...defultform
      },
      peoples: [], // 接收获取的员工简单列表的数据
      // 自定义校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          // 自定义函数校验
          { trigger: 'blur', validator: checkNameRepeat }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }

        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-50个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formDate.id ? '编辑部门' : '新增子部门'
    }
  },

  methods: {
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    async getDepartDetail(id) {
      this.formDate = await getDepartDetail(id)
      console.log(2)
    },
    btnOk() {
      this.$refs.deptForm.validate(async isOk => {
        if (isOk) {
          // 表示可以提交了
          if (this.formDate.id) {
            await updateDepartments(this.formDate)
            this.$message.success('修改成功')
          } else {
            await addDepartments({ ...this.formDate, pid: this.treeNode.id })
            this.$message.success('添加成功')
          }
          this.$emit('addDepts')
          this.showDialog = false
        }
      })
    },
    btnCancel() {
      this.formDate = { ...defultform }
      this.$refs.deptForm.resetFields()// 重置校验字段
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
